/**
 * 
 */
package fr.ants.view.panel;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.TransferHandler;

import fr.ants.controller.listener.ApplyButtonListener;
import fr.ants.controller.listener.ButtonsListener;
import fr.ants.model.Node;
import fr.ants.view.HMI;
import fr.ants.view.Selection;

/**
 * Tabbed pane in the control panel.
 * 
 * @author Guillaume Ruscassie
 */
public class TabbedPane extends JTabbedPane implements PropertyChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3345391544245161501L;
	/**
	 * simulation panel.
	 */
	private final SimulationPanel simulationPanel;
	/**
	 * nest panel.
	 */
	private final NestPanel nestPanel;

	/**
	 * Canvas.
	 */
	private final Canvas canvas;

	/**
	 * Build.
	 * @param canvas
	 *            Canvas.
	 * @param transferHandler
	 *            for the drag and drop.
	 * @param listener
	 *            for the buttons.
	 * 
	 */
	public TabbedPane(final Canvas canvas,
			final TransferHandler transferHandler,
			final ButtonsListener buttonsListener) {
		this.canvas = canvas;
		//Listener for the button apply
		final ApplyButtonListener applyButtonListener = new ApplyButtonListener();

		//Panel to manage the simulation
		simulationPanel = new SimulationPanel(applyButtonListener,
				buttonsListener, transferHandler);
		JScrollPane scrollPaneSimulator = new JScrollPane(simulationPanel,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		addTab(HMI.PARAMETER, scrollPaneSimulator);

		//panel to manage the nest
		nestPanel = new NestPanel(canvas, applyButtonListener, buttonsListener,
				transferHandler);
		final JScrollPane scrollPaneNest = new JScrollPane(nestPanel,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		addTab(HMI.NEST, scrollPaneNest);

	}

	@Override
	public void propertyChange(final PropertyChangeEvent evt) {
		if ("SelectedXY".equals(evt.getPropertyName())) {
			if (canvas != null
					&& canvas.getSelection().equals(Selection.SELECTION)
					&& canvas.getSelectedGridX() != null
					&& canvas.getSelectedGridY() != null
					&& canvas.getGrid() != null) {				
				
				Node node = canvas.getGrid().getNode(canvas.getSelectedGridX(),
						canvas.getSelectedGridY());
				if (node != null) {
					setSelectedIndex(0);
					simulationPanel.init(node);

					if (node.getNest() != null) {
						setSelectedIndex(1);
						nestPanel.init(node.getNest());
					}
				}
			}
		}
		if ("grid".equals(evt.getPropertyName())) {
			if (canvas.getGrid() == null) {
				simulationPanel.setEnabledButtonsTools(false);
				nestPanel.setEnabledButtonNest(false);
			} else {
				simulationPanel.setEnabledButtonsTools(true);
				nestPanel.setEnabledButtonNest(true);
			}
		}
		if ("Mode".equals(evt.getPropertyName())) {
			if (canvas.getGrid() != null) {
				simulationPanel.setActiveButtonTools(canvas.getSelection());
				simulationPanel.setActiveSliders(canvas.getSelection());
				nestPanel.setActiveButtonTools(canvas.getSelection());
				nestPanel.setActiveSliders(canvas.getSelection());
			}
		}
	}

	/**
	 * 
	 * @return the nest panel.
	 */
	public NestPanel getNestPanel() {
		return nestPanel;
	}

	/**
	 * @return the simulation panel.
	 */
	public SimulationPanel getSimulationPanel() {
		return simulationPanel;
	}
}